package gui.dialog;

import gui.dialog.ConnectionListener.ConnectionEvent;

import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import user.LocalUser;
import utils.Configuration;

public class ChatConnectDialog extends ChatDialog implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String CONNECT_TITLE = "Connexion" ;
	private static final Dimension CONNECT_SIZE = new Dimension(300, 200) ;
	
	private ConnectionListener connList ;
	private JButton connectButton ;
	private JButton cancelButton ;
	private JTextField fieldLogin ;
	
	private WindowListener windowAdapter = new WindowAdapter() {
		@Override
		public void windowActivated (WindowEvent e) {
			ChatConnectDialog.this.fieldLogin.setText(LocalUser.getLocalUser().getUsername()) ;
			ChatConnectDialog.this.fieldLogin.requestFocus() ;
		}
		@Override
		public void windowClosed(WindowEvent e) {		
			ChatConnectDialog.this.connList.actionConnect(new ConnectionEvent(this, false, true)) ;
		}
		@Override
		public void windowClosing(WindowEvent e) {		
			ChatConnectDialog.this.connList.actionConnect(new ConnectionEvent(this, false, true)) ;
		}
	};
	
	private KeyListener keyAdapater = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_ENTER:
				ChatConnectDialog.this.connectButton.doClick() ;
				break ;
			case KeyEvent.VK_ESCAPE:
				ChatConnectDialog.this.cancelButton.doClick() ;
				break ;
			default:
			}
		}
	};
	
	/**
	 * Create and return the information panel (Top of the dialog)
	 * 
	 * @return
	 */
	private JPanel createInfoPanel () {
		JPanel infoPanel = new JPanel() ;
		JLabel iconLabel = null ;
		iconLabel = new JLabel(new ImageIcon(Configuration.getImageFolder() + File.separator + "connect_img.png")) ; 
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.X_AXIS)) ;
		infoPanel.add(Box.createRigidArea(new Dimension(10, 0))) ;
		if (iconLabel != null) {
			iconLabel.setSize(new Dimension(20, 20)) ;
			infoPanel.add(iconLabel) ;
			infoPanel.add(Box.createRigidArea(new Dimension(10, 0))) ;
		}
		infoPanel.add(new JLabel("<html><p>Merci de spécifier un login correct afin de pouvoir accéder au système.</p></html>")) ;
		infoPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)) ;
		infoPanel.setAlignmentX(LEFT_ALIGNMENT) ;
		return infoPanel ;
	}
	
	/**
	 * Create and return the input panel (middle of the dialog).
	 * This functionc reate the JTextField attribute.
	 * 
	 * @return
	 */
	private JPanel createInputPanel () {
		JPanel loginPanel = new JPanel () ;
		this.fieldLogin = new JTextField(10) ;
		this.fieldLogin.addKeyListener(this.keyAdapater) ;
		loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.X_AXIS)) ;
		loginPanel.setAlignmentX(LEFT_ALIGNMENT) ; 
		loginPanel.add(Box.createRigidArea(new Dimension(20,0))) ;
		loginPanel.add(new JLabel("<html><b>Login : </b></html>")) ;
		loginPanel.add(Box.createHorizontalGlue()) ;
		loginPanel.add(this.fieldLogin) ;
		loginPanel.add(Box.createRigidArea(new Dimension(20,0))) ;
		loginPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, 50)) ;
		return loginPanel ;
	}
	
	/**
	 * Create and return the buttons panel (bottom of the dialog).
	 * This function create the JButton attributes.
	 * 
	 * @return
	 */
	private JPanel createButtonsPanel () {
		JPanel buttonPanel = new JPanel () ;
		this.connectButton = new JButton("Connexion") ;
		this.connectButton.addActionListener(this) ;
		this.cancelButton = new JButton("Annuler") ;
		this.cancelButton.addActionListener(this) ;
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)) ;
		buttonPanel.add(Box.createHorizontalGlue()) ;
		buttonPanel.add(this.connectButton) ;
		buttonPanel.add(Box.createHorizontalGlue()) ;
		buttonPanel.add(this.cancelButton) ;
		buttonPanel.add(Box.createHorizontalGlue()) ;
		buttonPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)) ;
		buttonPanel.setAlignmentX(LEFT_ALIGNMENT) ;
		return buttonPanel ;
	}
	
	public ChatConnectDialog (Window master, ConnectionListener cl) {
		super (master, CONNECT_TITLE, CONNECT_SIZE) ;
		this.connList = cl ;
		addWindowListener(this.windowAdapter) ;
		/* Create and place Component... */
		setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS)) ;
		add(createInfoPanel()) ;
		add(createInputPanel()) ;
		add(createButtonsPanel()) ;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.connectButton && !this.fieldLogin.getText().trim().isEmpty()) {
			this.connList.actionConnect(new ConnectionEvent(this, this.fieldLogin.getText().trim())) ;
		}
		else if (e.getSource() == this.cancelButton) {
			this.connList.actionConnect(new ConnectionEvent(this, true, false)) ;
			this.setVisible(false) ;
		}
	}

}
